package com.idanchuang.income;

import java.io.*;
import java.math.BigDecimal;
import java.time.YearMonth;
import java.util.*;

public class BrandInfo implements Serializable {
    static final long serialVersionUID = 0L;
    private static String[] types = {"自媒体", "社群管理", "宣传物料", "私享会"};
    private static String brandMonthlyRateFile = "files/品牌月度收益占比.csv";
    private static String brandTypeRateFile = "files/品牌类型收益占比.csv";
    private Map<YearMonth, List<BrandIncomeMonthlyRate>> brandsMonthlyRate;
    private Map<String, BigDecimal[]> brandTypeRate; //品牌的类型收益占比
    private Set<String> brands; //所有的品牌

    public BrandInfo() {
        this.brandsMonthlyRate = new HashMap<>();
        init();
    }

    private void init() {
        initBrandMonthRate();
        initBrandTypeRate();
    }

    private void initBrandTypeRate() {
        brandTypeRate = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(new File(brandTypeRateFile)))) {
            String row;
            while ((row = reader.readLine()) != null) {
                String[] data = row.split(",");
                String brand = data[0];
                BigDecimal[] rate = new BigDecimal[types.length];
                for (int i = 0; i < rate.length; i++) {
                    rate[i] = new BigDecimal(Float.valueOf(data[i + 1]));
                }
                brandTypeRate.put(brand, rate);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //剩余品牌的占比
        for (String brand : brands) {
            if (brandTypeRate.get(brand) == null) {
                brandTypeRate.put(brand, getDefaultBrandTypeRate());
            }
        }
    }

    private BigDecimal[] getDefaultBrandTypeRate() {
        return new BigDecimal[]{new BigDecimal(0.6), new BigDecimal(0.1), new BigDecimal(0.1), new BigDecimal(0.2)};
    }

    /**
     * 初始化品牌月度的占比
     */
    private void initBrandMonthRate() {
        brands = new HashSet<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(new File(brandMonthlyRateFile)))) {
            String row;
            while ((row = reader.readLine()) != null) {
                String[] data = row.split(",");
                BrandIncomeMonthlyRate rate = new BrandIncomeMonthlyRate();
                rate.setName(data[0]);
                String[] month = data[1].trim().split("-");
                rate.setMonth(YearMonth.of(Integer.valueOf(month[0].trim()), Integer.valueOf(month[1].trim())));
                rate.setRate(new BigDecimal(Float.valueOf(data[2])));
                List<BrandIncomeMonthlyRate> rates = brandsMonthlyRate.get(rate.getMonth());
                if (rates == null) {
                    rates = new LinkedList<>();
                    brandsMonthlyRate.put(rate.getMonth(), rates);
                }
                brands.add(rate.getName());
                rates.add(rate);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Map<YearMonth, List<BrandIncomeMonthlyRate>> getBrandsMonthlyRate() {
        return brandsMonthlyRate;
    }

    public void setBrandsMonthlyRate(Map<YearMonth, List<BrandIncomeMonthlyRate>> brandsMonthlyRate) {
        this.brandsMonthlyRate = brandsMonthlyRate;
    }

    public Map<String, BigDecimal[]> getBrandTypeRate() {
        return brandTypeRate;
    }

    public void setBrandTypeRate(Map<String, BigDecimal[]> brandTypeRate) {
        this.brandTypeRate = brandTypeRate;
    }

    //
//
//    private static Map<String, YearMonth> detail = new HashMap<>() {{
//        put("五粮液", YearMonth.of(2020, 10));
//        put("贵州习酒", YearMonth.of(2020, 9));
//        put("大摩", YearMonth.of(2020, 9));
//        put("BEEOTIC", YearMonth.of(2020, 9));
//        put("Thorne", YearMonth.of(2020, 7));
//        put("Amilera", YearMonth.of(2020, 5));
//        put("DOVERIST", YearMonth.of(2020, 5));
//        put("VANDENBERG", YearMonth.of(2020, 5));
//        put("Alissi Bronte", YearMonth.of(2020, 3));
//        put("ch22", YearMonth.of(2019, 12));
//        put("TORRES", YearMonth.of(2019, 11));
//    }};

    public static class BrandIncomeMonthlyRate implements Comparable<BrandIncomeMonthlyRate>, Cloneable, Serializable {
        static final long serialVersionUID = 0L;
        private String name;
        private YearMonth month;
        private BigDecimal rate; //品牌月度收益占比
        private BigDecimal[] typeRates; //分类占比

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public YearMonth getMonth() {
            return month;
        }

        public void setMonth(YearMonth month) {
            this.month = month;
        }

        public BigDecimal getRate() {
            return rate;
        }

        public void setRate(BigDecimal rate) {
            this.rate = rate;
        }

        public BigDecimal[] getTypeRates() {
            return typeRates;
        }

        public void setTypeRates(BigDecimal[] typeRates) {
            this.typeRates = typeRates;
        }

        @Override
        public int compareTo(BrandIncomeMonthlyRate o) {
            return this.rate.compareTo(o.rate);
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            BrandIncomeMonthlyRate rate = new BrandIncomeMonthlyRate();
            rate.rate = new BigDecimal(this.rate.floatValue());
            rate.name = this.name;
            rate.month = this.month;
            return rate;
        }
    }

}
